package com.jcl.recruit.api.test.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.security.SecurityUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.SearchParamEntity;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.service.UserService;
import com.jcl.recruit.wechat.service.WechatPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApiTestController
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/29 10:18
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/test")
public class ApiTestController {
    private static final Logger logger = LoggerFactory.getLogger(ApiTestController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private WechatPushService wechatPushService;
    @Autowired
    private SearchResumeServer searchResumeServer;

    @RequestMapping("/elastic/access")
    public ApiResponse elastic() {

        List<SearchResumeRecord> recordList = new ArrayList<>();

        SearchParamEntity entity = new SearchParamEntity();
        entity.setResumeRecords(recordList);
        searchResumeServer.resumePageList(entity);

        return ApiResponse.success(null);
    }

    @RequestMapping("/wechat/push")
    public ApiResponse timer(Integer userId) {
        if (null == userId) {
            return ApiResponse.error("用户参数不能为空！");
        }
        int number = wechatPushService.sendMessage(userId);
        if (0 == number) {
            return ApiResponse.success("操作成功");
        } else {
            return ApiResponse.success("推送失败，请查看用户参数是否正确！");
        }
    }

    @RequestMapping("/wechat/push/crawling/data")
    public ApiResponse timerPushCrawlingDataTest() {
        List<Map<String, String>> list = searchResumeServer.getCountBySources(true);
        wechatPushService.resumeCountSendMessage(list);
        return ApiResponse.success("操作成功");

    }

    @RequestMapping("/wechat/access")
    public void access(String userId, HttpServletResponse response) {
        String domain = SystemConfig.getSystemWebUrl();
        if (StringUtils.isEmpty(userId)) {
            return;
        }
        User user = userService.findOneById(Integer.parseInt(userId));
        if (null != user) {
            String token = SecurityUtils.encodeUserToken(user.getCorpCode(), user.getId().toString(), null, null, null);
            logger.info("access test token is : {}", token);
            user.setToken(token);
            try {
                response.sendRedirect(domain + "/mb/mbindex.html#/?token=" + user.getToken());
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }


}
